<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never" v-loading="loading">
        <!-- 搜索栏 -->
        <el-form
          :inline="true"
          :model="pagination"
          ref="pagination"
        >
          <el-form-item label="服务商名称:" prop="code">
            <el-input v-model="pagination.storeName" placeholder="请输入服务商名称"></el-input>
          </el-form-item>
          <el-form-item label="服务项目编号：" prop="code">
            <el-input v-model="pagination.code" placeholder="请输入服务项目编号"></el-input>
          </el-form-item>
          <el-form-item label="服务项目名称：" prop="name">
            <el-input v-model="pagination.name" placeholder="请输入服务项目名称"></el-input>
          </el-form-item>
          <el-form-item label="服务分类：" prop="type1">
            <el-select v-model="pagination.subServiceTypeId" placeholder="请选择">
              <el-option
                v-for="item in typeLab"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleRest">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 搜索栏 / -->
<!--        <div class="btnBox" style="text-align:right">-->
<!--          <el-button type="primary" @click="handleAdd">新增</el-button>-->
<!--        </div>-->
        <!-- 数据表格 -->
        <el-table
          :data="items"
          border
          style="width: 100%; margin-top:10px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="id"></el-table-column>
          <el-table-column prop="store.name" label="服务商名称"></el-table-column>
          <el-table-column prop="code" label="服务编码"></el-table-column>
          <el-table-column prop="name" label="服务名称"></el-table-column>
          <el-table-column prop="serviceSubType.name" label="服务分类"></el-table-column>
          <el-table-column prop="normalPrice" label="价格"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" type="text" size="small">查看</el-button>
              <el-button @click="handleDelect(scope.row)" type="text" size="small">下架</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        ></el-pagination>
        <!-- 数据表格 / -->
      </el-card>
      <!-- 新增标签弹层 -->
      <Dialog ref="addType" :dialogTitle="dialogTitle" v-on:newDataes = subMitForm></Dialog>
    </div>
  </div>
</template>

<script>
import { getList, getDetails, updateData } from '@/api/service'
import { getSubSimList } from '@/api/serviceType'
import Dialog from './../components/serviceAdd'
export default {
  name: 'serviceList',
  components: {
    Dialog
  },
  data() {
    return {
      items: [{}],
      total: 20,
      pagination: {
        page: 1,
        pageSize: 10,
        subServiceTypeId: '',
        storeName: '',
        code: '',
        name: '',
        status: 1
      },
      loading: false,
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: '',
      typeLab: []
    }
  },
  created() {
    this.doQuery(this.pagination)
    this.getTyleList()
  },
  methods: {
    // 业务方法
    async doQuery(parent) {
      this.loading = true
      await getList(parent)
        .then(res => {
          const { data } = res
          this.items = data.items
          this.total = data.counts
          this.loading = false
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
    },
    async getTyleList() {
       await getSubSimList().then(res => {
         this.typeLab = res.data
       })
    },
    // UI方法 - 重置
    handleRest() {
      this.pagination.storeName = ''
      this.pagination.subServiceTypeId = ''
      this.$refs['pagination'].resetFields()
    },
    // 搜索
    handleSearch() {
      this.doQuery(this.pagination)
    },
    //
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    // 每页条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.doQuery(this.pagination)
    },
    // 当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pagination.page = val
      this.doQuery(this.pagination)
    },
    // 添加
    handleAdd(val) {
      this.dialogTitle = '添加'
      this.$refs.addType.dialogFormV()
    },
    // 详情获取  - 如果有详情接口 读详情
    async getDetails(id) {
      let dataes = {}
      await getDetails(id)
        .then(res => {
          const { data } = res
          this.loading = false
          dataes = data
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
      return dataes
    },
    // 编辑
    async handleEdit (val) {
      this.dialogTitle = '详情'
      // 如果有详情接口 读详情
      const data = await this.getDetails(val.id)
      // 如果列表中的数据可以满足修改的信息展示  则不用读取上面的详情接口 用下面的data
      // const data = val
      this.$refs.addType.dialogFormV(data)
    },
    // 删除
    handleDelect(item) {
      this.$confirm('是否确认下架？')
        .then(async ret => {
          await updateData({id: item.id, status: 2})
            .then(res => {
              this.loading = false
              this.$message({
                message: '下架成功！',
                type: 'success'
              })
              this.doQuery(this.pagination)
            })
            .catch(err => {
              console.log(err)
              this.loading = false
            })
        })
        .catch(ret => {
          console.log(ret)
        })
    },
    // subMitForm
    subMitForm(val) {
      if (val.id) {
        this.$message.success('修改成功')
      } else {
        this.$message.success('添加成功')
      }
      this.doQuery(this.pagination)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px 0px 0px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
